package com.gt.quality;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * ---'<a href="https://www.cnblogs.com/gtnotgod">白嫖老郭</a>'
 *
 * @author: makeJava
 * @createTime: 2026-02-26 21:50
 * @since JDK 21 And SpringBoot 3.X config
 */
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.gt.quality.system.mapper")
@EnableScheduling  // 启用定时任务
@EnableAspectJAutoProxy
@EnableCaching
public class WebApplication {

    private static final Logger logger = LoggerFactory.getLogger(WebApplication.class);

    /**
     * ---'<a href="https://www.cnblogs.com/gtnotgod">白嫖老郭</a>'
     * 账号 admin - 密码: admin
     *
     * @param args args
     * @author: makeJava
     * @createTime: 2026-12-26 21:50
     */
    public static void main(String[] args) throws SocketException {
        // 启动Spring Boot应用
        SpringApplication sp = new SpringApplication(WebApplication.class);
        sp.addListeners(new ApplicationPidFileWriter());
        // 禁用默认的关闭钩子
        sp.setRegisterShutdownHook(false);
        ConfigurableApplicationContext context = sp.run(args);
        // 获取Environment实例
        Environment env = context.getEnvironment();
        // 获取端口号和上下文路径
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        // 获取本机所有网络接口
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            // 获取每个网络接口的所有IP地址
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                // 只输出IPv4地址
                if (inetAddress instanceof Inet4Address && (StringUtils.startsWithAny(networkInterface.getName(), "wireless_", "wlan", "wifi"))) {
                    String localHostIp = inetAddress.getHostAddress();
                    logger.info("-------->后台地址：https://{}:{}{}", localHostIp, serverPort, contextPath);
                }
            }
        }
        logger.info("lay-Super-manager-System 启动成功......");
    }

}
